package com.alasbusy.alasbusyapp.activity.twoactivity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.base.BaseActivity;
import com.alasbusy.alasbusyapp.bean.HomeBean;

import java.util.ArrayList;
import java.util.List;

public class PictureDisplayActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    //private SimpleDraweeView imageView;
    private TextView pictureDisplayText;
    private ViewPager onthe_viewpager;
    public List<HomeBean> list = new ArrayList<>();
    public List<Integer> ImagePage = new ArrayList<>();
    private List<ImageView> list_view = new ArrayList<>();
    private int currentIndex;

    @Override
    protected void initView() {
        /*imageView = (SimpleDraweeView) findViewById(R.id.picture_display_image);*/
        onthe_viewpager = findViewById(R.id.onthe_viewpager);
        pictureDisplayText = (TextView) findViewById(R.id.picture_display_text);


        ImagePage.clear();
        list_view.clear();
        ImagePage.add(R.mipmap.viewpager_01);
        ImagePage.add(R.mipmap.viewpager_02);
        ImagePage.add(R.mipmap.viewpager_03);
        RelativeLayout.LayoutParams layouts = new RelativeLayout.LayoutParams(
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        for (int i = 0; i < ImagePage.size(); i++) {
            ImageView image = new ImageView(this);
            image.setImageResource(ImagePage.get(i));
            //image.setScaleType(ImageView.ScaleType.FIT_XY);
            image.setLayoutParams(layouts);
            list_view.add(image);
        }

        currentIndex = 0;
        pictureDisplayText.setText(""+(currentIndex+1)+"/"+ImagePage.size());
        onthe_viewpager.setOnPageChangeListener(this);

        ViewPagerAdapter adapter = new ViewPagerAdapter(list_view);
        onthe_viewpager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_picture_display;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        setCurrentDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > list_view.size() - 1
                || currentIndex == position) {
            return;
        }
        pictureDisplayText.setText(""+(position+1)+"/"+ImagePage.size());

        currentIndex = position;
    }

    private class ViewPagerAdapter extends PagerAdapter {
        private final List<ImageView> views;

        public ViewPagerAdapter(List<ImageView> list_view) {
            this.views = list_view;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //((ViewPager) container).removeView(views.get(position));
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 将当前视图添加到container中
            //((ViewPager) container).addView(views.get(position));
            View v = views.get(position);
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }
            container.addView(views.get(position));
            // 设置当前视图的唯一标示Key
            return views.get(position);
        }
    }
}
